{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 10\n",
    "\n",
    "# 函数单调性\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b761300-b5a8-4b68-8b40-002f8e2f5089",
   "metadata": {},
   "source": [
    "这段代码使用SymPy库来判断两个不同表达式在指定区间内的增减性。代码的核心是使用`is_increasing`函数分析每个表达式在给定区间上是否是单调递增的。\n",
    "\n",
    "1. **表达式1**：$$-x^2$$\n",
    "   - 该表达式在区间$(-\\infty, 0)$上是单调递增的，因为随着$x$从负无穷趋近于0，$-x^2$的值在不断增加。而在区间$(0, \\infty)$上，该表达式是单调递减的，因为$x$的增大会导致$-x^2$值减少。\n",
    "\n",
    "2. **表达式2**：$$-x^2 + y$$\n",
    "   - 在此表达式中，$y$是一个常数项，仅$x$是变量。代码判断其在区间$(-1, 2)$上对变量$x$的增减性。由于$-x^2$是一个开口向下的抛物线，因此表达式在区间$(-1, 2)$上不会是单调递增的。\n",
    "\n",
    "代码输出每个表达式在不同区间内的增减性，为分析表达式的单调性提供了定量依据。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59898e64-7527-4481-a74b-7125abf795cd",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd84786c-759e-4849-8570-6fb45993f3eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import is_increasing  # 导入is_increasing函数用于判断表达式的增减性\n",
    "from sympy.abc import x, y  # 定义符号变量x和y\n",
    "from sympy import Interval, oo  # 导入Interval用于区间定义，oo表示无穷大"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afd830cc-0c47-40d8-bf99-2b219a5272ef",
   "metadata": {},
   "source": [
    "## 定义表达式1并判断其增减性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "34636bb4-1dbd-4da0-b067-d9934198a7e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - x^{2}$"
      ],
      "text/plain": [
       "-x**2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr_1 = -x**2  # 定义表达式1\n",
    "expr_1  # 输出表达式1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67d74dc7-4c74-4b6d-81ef-af2cea319865",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(is_increasing(expr_1, Interval(-oo, 0)))  # 判断表达式1在区间(-∞, 0)上的增减性\n",
    "print(is_increasing(expr_1, Interval(0, oo)))  # 判断表达式1在区间(0, ∞)上的增减性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3ff0b2c-1f0f-47a9-9b8b-00505710714b",
   "metadata": {},
   "source": [
    "## 定义表达式2并判断其增减性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "99ad464f-6df0-4fde-bc9e-e0b329e7b4fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - x^{2} + y$"
      ],
      "text/plain": [
       "-x**2 + y"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr_2 = -x**2 + y  # 定义表达式2\n",
    "expr_2  # 输出表达式2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cffe6d73-17d5-4b25-b48f-7819f182e17f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(is_increasing(expr_2, Interval(-1, 2), x))  # 判断表达式2在区间(-1, 2)上对变量x的增减性"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
